﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "GDChatItemView.h"
#include "../Util/GDUtil.h"

GDChatItemView* GDChatItemView::createWithFrame(const CCRect& rect, std::string msg, bool isMe)
{
	GDChatItemView * pRet = new GDChatItemView();
	if (pRet && pRet->initWithFrame(rect))
	{
		pRet->initView(msg, isMe);
		pRet->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(pRet);
	}
	return pRet;
}

GDChatItemView::GDChatItemView(void)
:msgLabel(NULL)
{
}


GDChatItemView::~GDChatItemView(void)
{
}

void GDChatItemView::initView(std::string msg, bool isMe)
{
	if (!isMe){
		msg = "geelou：" + msg;
	}
	CCSize winSize = this->getBounds().size;
	int maxWidth = winSize.width - _px(40);  // 最大宽度

	int msgWidth = CAImage::getStringWidth("", _px(20), msg) + _px(30);

	CAScale9ImageView *bgView = CAScale9ImageView::createWithImage(CAImage::create("source_material/textField_bg.png"));

	if (msgWidth > maxWidth){
		msgWidth = maxWidth;
	}

	if (isMe){
		bgView->setFrame(CCRect(winSize.width - msgWidth - _px(10), 0, msgWidth, 50));
	}
	else{
		bgView->setFrame(CCRect(_px(10), 0, msgWidth, 50));
	}
	this->addSubview(bgView);

	msgLabel = GDUtil::createLabelWithFrame(CCRect(10, 15, msgWidth-_px(10), 60), msg.c_str(), isMe ? CAColor_blue : CAColor_black, _px(20), "",CATextAlignmentLeft);
	bgView->addSubview(msgLabel);

	int height =  GDUtil::getLabelHeight(msgLabel);
	GDUtil::setViewHeight(msgLabel, height+10);
	GDUtil::setViewHeight(bgView, height + 30);
	GDUtil::setViewHeight(this, height + 30);

	if (!isMe){
		CALabel *nameLabel = GDUtil::createLabelWithFrame(msgLabel->getBounds(), "geelou：", CAColor_red, _px(20), "", CATextAlignmentLeft);
		msgLabel->addSubview(nameLabel);
	}
}